package net.tycmc.cty.ctyms.timejob.iems.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tycmc.functions.MapFunction;

import net.tycmc.bulb.common.util.DateUtil;
import net.tycmc.bulb.common.util.MapGetter;
import net.tycmc.bulb.common.util.StringUtil;
import net.tycmc.cty.ctyms.timejob.common.DateControl;
import net.tycmc.cty.ctyms.timejob.common.DateList;
import net.tycmc.cty.ctyms.timejob.function.mail.SendMail;
import net.tycmc.cty.ctyms.timejob.iems.dao.DailyMaillConn;


/**
 * 邮件日报
 * @author lixiaofan
 *
 */
public class DailyMaillService {
	DailyMaillConn dmc;
	public DailyMaillConn getDmc() {
		return dmc;
	}
	public void setDmc(DailyMaillConn dmc) {
		this.dmc = dmc;
	}
	/**
	 * 发送邮件
	 */
	public void sendMail(){
		//获取提示设定中邮件日报定制不为空人员信息集合
		List<Map<String,Object>> userList = dmc.getUserList();
		if(userList != null && userList.size()>0){
			int num=0;
			for (Map<String,Object> user:userList) {
				//Codes
				String Codes = MapGetter.getString(user, "OrganizationCode");
				//Email
				String Email = MapGetter.getString(user, "Email");
				//人员ID
				String UserID = MapGetter.getString(user, "UserID");
				//人员名称
				String UserName = MapGetter.getString(user, "UserName");
				//提示时间
				String PromptTime = MapGetter.getString(user, "PromptTime");
				//邮件开头
				String top = "尊敬的"+UserName+"客户，您好！  "+DateControl.getFormatDate(PromptTime, 3)+" IEMS系统日报如下，请注意。<br /><br/>";
				//汇总
				String collect = "<b>汇总：</b><br />";
				
				//明细
				StringBuffer detail = new StringBuffer();
				detail.append("<br /><b>明细：</b><br />");
				
				String SMSAS_MailService = MapGetter.getString(user, "SMSAS_MailService");	//服务期
				if(SMSAS_MailService.equals("1") || SMSAS_MailService.equals("true")){	//服务期信息
					collect = getService(Codes,collect,detail);
				}
				collect = getOtherPrompt(Codes,collect,detail,user);
				List<String> list = new ArrayList<String>();
				list.add(Email);
//				list.add("fujichuan@tycmc.net");
//				list.add("lixiaofan@tycmc.net");
				int result = SendMail.getInstance().sendMail("IEMS "+DateControl.getFormatDate(PromptTime, 3)+" 系统提示日报", top+collect+detail.toString(), list);
				num+=result;
				if(result==2){
					System.out.println("给人员："+UserName+"，人员ID："+UserID+" 发送到"+Email+"-------失败!");
				}
			}
			System.out.println(DateUtil.toString(DateUtil.now())+"共发送邮件"+num+"封；共需要发送邮件"+userList.size()+"封。");
		}
	}
	
	/**
	 * 获取服务期信息
	 * @param dbCode	人员机构Code
	 * @param collect	汇总内容
	 * @param detail	明细内容
	 */
	public String getService(String dbCode,String collect,StringBuffer detail){
		List<Map<String,Object>> list = dmc.getServiceList(dbCode);
		int num = 0;
		if(list != null && list.size()>0){
			num = list.size();
			//增加明细
			detail.append("服务期提示：<br /><table border=\"1\" cellpadding=\"0\" cellspacing=\"0\"   ><tr><th>设备号</th><th>服务期到期时间</th></tr>");
			for (Map<String,Object> map:list) {
				//设备号
				String VehicleNumber = MapGetter.getString(map, "VehicleNumber");
				//服务期到期时间
				String ServiceExp = MapGetter.getString(map, "ServiceExp");
				
				detail.append("<tr>");
				detail.append("<td>"+VehicleNumber+"</td>");
				detail.append("<td>"+ServiceExp+"</td>");
				detail.append("</tr>");
			}
			detail.append("</table><br />");
		}
		
		return collect+="服务期："+num+"条<br />";
	}
	
	/**
	 * 获取其他提示类型的详细信息
	 * @param dbCode
	 * @param collect
	 * @param detail
	 * @param user
	 * @return
	 * @更新 20140627 jiyongtian 保养提示 取值调整
	 *      20140704 jiyongtian 新增传感器报警
	 */
	public String getOtherPrompt(String dbCode,String collect,StringBuffer detail,Map<String,Object> user){
		String SMSAS_MailMtn = MapGetter.getString(user, "SMSAS_MailMtn");	//保养提示
//		String SMSAS_MailMtnNew = MapGetter.getString(user, "SMSAS_MailMtnNew");	//保养提示
		String SMSAS_MailFlt = MapGetter.getString(user, "SMSAS_MailFlt");	//故障提示
		String SMSAS_MailRels = MapGetter.getString(user, "SMSAS_MailRels");	//故障解除提示
		String SMSAS_MailAlm = MapGetter.getString(user, "SMSAS_MailAlm");	//报警提示
		/*String SMSAS_MailBehavior = MapGetter.getString(user, "SMSAS_MailBehavior");	//驾驶行为提示
		String SMSAS_MailLoad = MapGetter.getString(user, "SMSAS_MailLoad");	//载重监控
*/		String SMSAS_MailMkbj = MapGetter.getString(user, "SMSAS_MailMkbj");	//门控报警
		String SMSAS_MailSmoke = MapGetter.getString(user, "SMSAS_MailSmokeAlarm");//烟度报警
		String SMSAS_MailSmokeRem = MapGetter.getString(user, "SMSAS_MailSmokeRels");//烟度报警解除
		String SMSAS_MailTemp = MapGetter.getString(user, "SMSAS_MailTempAlarm");//温度报警
		String SMSAS_MailTempRem = MapGetter.getString(user, "SMSAS_MailTempRels");//温度报警解除
		/*3.6.6新增进出围栏提示和时效预警 20141018-wangkai*/
		String SMSAS_MailFenTip = MapGetter.getString(user, "SMSAS_MailFenTip");//进出围栏提示
		String SMSAS_MailTankLevel = MapGetter.getString(user, "SMSAS_MailTankLevel");//4.19新增液位传感器报警
		
		List<Map<String,Object>> list = dmc.getPromptList(dbCode);
		if(SMSAS_MailMtn.equals("true")){
			list.addAll(dmc.getPromptOfMainAlert(dbCode));
		}
		Map<String,Object> numList = new HashMap<String,Object>();

		//获取提示类型（1：故障；2：报警；3：门控；4：传感器；5：驾驶行为；6：自动安装；7：进出围栏、时效性提示及时效性统计，的集合
		Map<String,List<String>> promptSwitch=dmc.getPromptSwitch();
		
		if(list != null && list.size()>0){
			
			boolean isFlt = SMSAS_MailFlt.equals("true");
			boolean isRels = SMSAS_MailRels.equals("true");
			
			String Mtn = "";	//保养提示台数
			StringBuffer detail_Mtn = new StringBuffer();
			detail_Mtn.append("保养提示：<br /><table border=\"1\" cellpadding=\"0\" cellspacing=\"0\"   ><tr><th>提示时间</th><th>设备号</th><th>设备型号</th><th>保养方式</th><th>保养内容</th></tr>");
			String FltVcl = "";	//故障提示台数
			int FltItem = 0;	//故障提示条数
			String RelsVcl = "";	//故障解除台数
			int RelsItem = 0;	//故障解除条数
			StringBuffer detail_FltRels = new StringBuffer();
			detail_FltRels.append("故障/解除提示：<br /><table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" ><tr><th>设备号</th><th>发动机号</th><th>故障时间</th><th>故障时位置</th>" +
					"<th>解除时间</th><th>故障码</th><th>故障颜色</th><th>车辆联系人</th><th>电话</th><th>故障描述</th></tr>");
			
			String AlmVcl = "";	//报警提示台数
			int AlmItem = 0;	//报警提示条数
			StringBuffer detail_Alm = new StringBuffer();
			detail_Alm.append("报警提示：<br /><table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" ><tr><th>设备号</th><th>报警时间</th>" +
			"<th>报警项目</th></tr>");
			
			String MkbjVcl = "";	//门控报警台数
			int MkbjItem = 0;	//门控报警条数
			StringBuffer detail_Mkbj = new StringBuffer();
			detail_Mkbj.append("门控报警：<br /><table border=\"1\" cellpadding=\"0\" cellspacing=\"0\"   ><tr><th>设备号</th><th>报警时间</th>" +
			"<th>报警类型</th><th>详细信息</th></tr>");
			
			String SensorVcl = "";
			int countSensor = 0;
			StringBuffer detail_sensor = new StringBuffer();//传感器报警
			detail_sensor.append("传感器报警：<br /><table border=\"1\" cellpadding=\"0\" cellspacing=\"0\"  ><tr><th>ID</th><th>设备号</th>" +
					"<th>报警时间</th><th>提示类型</th><th>详细信息</th><th>解除时间</th><th>解除方式</th>");
			
			String FenTipVcl=""; //进出围栏台数
			int FenTipItem = 0;	//进出围栏提示条数
			StringBuffer detail_FenTip = new StringBuffer();
			detail_FenTip.append("进出围栏提示：<br /><table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" ><tr><th>设备号</th><th>发动机号</th><th>提示时间</th><th>提示类型（进围栏提示、出围栏提示）</th>" +
					"<th>提示围栏</th><th>提示时位置</th></tr>");
			
			String TankVcl="";//液位传感器报警台数
			int TankItem=0;//液位传感器报警提示条数
			StringBuffer detail_Tank = new StringBuffer();
			detail_Tank.append("燃油异常减少：<br /><table border=\"1\" cellpadding\"0\" cellspacing=\"0\"><tr><th>设备号</th><th>报警时间</th><th>报警类型</th><th>报警地点</th><th>详细信息</th></tr>");
			
			for (Map<String,Object> map:list) {
				if(!StringUtil.isValid(MapGetter.getString(map, "MsgP_Lo"))){
					map.put("MsgP_Lo", "0.0");
				}
				if(!StringUtil.isValid(MapGetter.getString(map, "MsgP_LA"))){
					map.put("MsgP_LA", "0.0");
				}
				//提示类型（1：故障；2：报警；3：保养；4：司机行为；5：载重监控；9 门控报警；15 进出围栏；16 时效预警）
				String MsgP_Type = MapGetter.getString(map, "MsgP_Type");
				//设备号
				String VehicleNumber = MapGetter.getString(map, "VehicleNumber");
				//设备id
				String MsgP_VclID= MapGetter.getString(map, "MsgP_VclID");
				//详细信息
				String MsgP_DetailMsg = MapGetter.getString(map, "MsgP_DetailMsg");
				Map<String,Object> detailMsgs = getDetailMsg(MsgP_Type,MsgP_DetailMsg);
				//提示时间
				String MsgP_MsgTime = MapGetter.getString(map, "MsgP_MsgTime");
				if(MsgP_Type.equals("3") && SMSAS_MailMtn.equals("true") && StringUtil.isValid(MapGetter.getString(map, "mtnal_alarmtype"))){	//保养提示
//				if(MsgP_Type.equals("3") && SMSAS_MailMtnNew.equals("true")){
					if(!Mtn.contains(VehicleNumber+",")){
						Mtn += VehicleNumber+",";
					}
					detail_Mtn.append("<tr>");
					detail_Mtn.append("<td>"+MapGetter.getString(map, "MtnAL_AlarmDate")+"</td>");
					detail_Mtn.append("<td>"+VehicleNumber+"</td>");
					detail_Mtn.append("<td>"+MapGetter.getString(map,"vehicletypename")+"</td>");
					detail_Mtn.append("<td>"+MapGetter.getString(map,"MtnAL_AlarmTypeName")+"</td>");
					detail_Mtn.append("<td>"+MapGetter.getString(map,"MtnAL_AlarmValue").replaceAll(" ", "")+"</td>");
					detail_Mtn.append("</tr>");
				}else if(MsgP_Type.equals("1") && (SMSAS_MailFlt.equals("true") || SMSAS_MailRels.equals("true"))){	//故障/解除提示
						//判断是否存在设备提示开关为关的设备id如果存在则不处理-wangkai20141020-3.6.6新增
						if(!promptSwitch.get("1").contains(MsgP_VclID)){
							//故障状态类型
							String MsgP_State = MapGetter.getString(map, "MsgP_State");
							//故障解除时间
							String MsgP_ReleaseTime = MapGetter.getString(map, "MsgP_ReleaseTime");
							String position = "暂无位置";
							String MsgP_Lo=MapGetter.getString(map, "MsgP_Lo");
							String MsgP_LA=MapGetter.getString(map, "MsgP_LA");
							try {
								position = MapFunction.getPositionReviseNoReplaceDescription(MsgP_Lo,MsgP_LA);
							} catch (Exception e) {
								//调用出错
								System.out.print("调用地图服务获得位置出错，" +
										"代码位置：DailyMaillService.getOtherPrompt(String dbCode,String collect,StringBuffer detail,Map<String,Object> user)");
							}
							position = position.replaceAll("省", "").replaceAll("市", "").replaceAll("附近","").replaceAll(" ","");
							if(position.length()>15){
								position = position.substring(0,15);
							}
							if("3".equals(MsgP_State) && isRels){	//已解除
								RelsItem++;
								if(!RelsVcl.contains(VehicleNumber+",")){
									RelsVcl += VehicleNumber+",";
								}
								detail_FltRels.append("<tr>");
								detail_FltRels.append("<td>"+VehicleNumber+"</td>");
								detail_FltRels.append("<td>"+MapGetter.getString(map, "ESNNumber")+"</td>");
								detail_FltRels.append("<td>"+MsgP_MsgTime+"</td>");
								detail_FltRels.append("<td>"+position+"</td>");
								detail_FltRels.append("<td>"+MsgP_ReleaseTime+"</td>");
								detail_FltRels.append("<td>"+MapGetter.getString(detailMsgs, "FltCode")+"</td>");
								detail_FltRels.append("<td>"+getFltColor(detailMsgs)+"</td>");
								detail_FltRels.append("<td>"+MapGetter.getString(map, "VehicleContact")+"</td>");
								detail_FltRels.append("<td>"+MapGetter.getString(map, "VehicleChargedMobile")+"</td>");
								detail_FltRels.append("<td>"+MapGetter.getString(detailMsgs, "FltContent")+"</td>");
								detail_FltRels.append("</tr>");
							}else if("1".equals(MsgP_State) && isFlt){//报警中
								FltItem++;
								if(!FltVcl.contains(VehicleNumber+",")){
									FltVcl += VehicleNumber+",";
								}
								detail_FltRels.append("<tr>");
								detail_FltRels.append("<td>"+VehicleNumber+"</td>");
								detail_FltRels.append("<td>"+MapGetter.getString(map, "ESNNumber")+"</td>");
								detail_FltRels.append("<td>"+MsgP_MsgTime+"</td>");
								detail_FltRels.append("<td>"+position+"</td>");
								detail_FltRels.append("<td>"+MsgP_ReleaseTime+"</td>");
								detail_FltRels.append("<td>"+MapGetter.getString(detailMsgs, "FltCode")+"</td>");
								detail_FltRels.append("<td>"+getFltColor(detailMsgs)+"</td>");
								detail_FltRels.append("<td>"+MapGetter.getString(map, "VehicleContact")+"</td>");
								detail_FltRels.append("<td>"+MapGetter.getString(map, "VehicleChargedMobile")+"</td>");
								detail_FltRels.append("<td>"+MapGetter.getString(detailMsgs, "FltContent")+"</td>");
								detail_FltRels.append("</tr>");
							}else{
								continue;
							}
					  }
				}else if(MsgP_Type.equals("2") && SMSAS_MailAlm.equals("true")){	//报警提示
					//判断是否存在设备提示开关为关的设备id如果存在则不处理-wangkai20141020-3.6.6新增
					if(!promptSwitch.get("2").contains(MsgP_VclID)){
						if(!AlmVcl.contains(VehicleNumber+",")){
							AlmVcl += VehicleNumber+",";
						}
						AlmItem ++;
						detail_Alm.append("<tr>");
						detail_Alm.append("<td>"+VehicleNumber+"</td>");
						detail_Alm.append("<td>"+MsgP_MsgTime+"</td>");
						detail_Alm.append("<td>"+MsgP_DetailMsg+"</td>");
						detail_Alm.append("</tr>");
					}
				}else if(MsgP_Type.equals("9") && SMSAS_MailMkbj.equals("true")){	//门控报警
					//判断是否存在设备提示开关为关的设备id如果存在则不处理-wangkai20141020-3.6.6新增
					if(!promptSwitch.get("3").contains(MsgP_VclID)){
						if(!MkbjVcl.contains(VehicleNumber+",")){
							MkbjVcl += VehicleNumber+",";
						}
						MkbjItem++;
						String Msgp_SecTypeID = MapGetter.getString(map, "Msgp_SecTypeID");
						String DetailMsg = VehicleNumber+"在"+DateList.getFormatDate(MsgP_MsgTime, 10)+"检测到门控异常开启车门报警，请注意";
						detail_Mkbj.append("<tr>");
						detail_Mkbj.append("<td>"+VehicleNumber+"</td>");
						detail_Mkbj.append("<td>"+MsgP_MsgTime+"</td>");
						detail_Mkbj.append("<td>"+(Msgp_SecTypeID.equals("1")?"在允许区域超速时开启车门":Msgp_SecTypeID.equals("0")?"在非允许区域开启车门":"")+"</td>");
						detail_Mkbj.append("<td>"+DetailMsg+"</td>");
						detail_Mkbj.append("</tr>");
					}
				}else if(MsgP_Type.equals("14") && (SMSAS_MailSmoke.equals("true")
						||SMSAS_MailSmokeRem.equals("true")||SMSAS_MailTemp.equals("true")||SMSAS_MailTempRem.equals("true"))){	//传感器报警
					//判断是否存在设备提示开关为关的设备id如果存在则不处理-wangkai20141020-3.6.6新增
					if(!promptSwitch.get("4").contains(MsgP_VclID)){
						String MsgP_SecTypeId = MapGetter.getString(map, "MsgP_SecTypeId");
						String MsgP_State = MapGetter.getString(map, "MsgP_State");
						String release = "自动";
						String detailsenor = "";
						boolean flag = false;
						if(MsgP_SecTypeId.equals("17")){//温度
							detailsenor = "温度报警，"+MapGetter.getString(map, "MsgP_SensorValue")+"℃";
							if(MsgP_State.equals("1")&&SMSAS_MailTemp.equals("true")){
								flag = true;
							}
							if(MsgP_State.equals("3")&&SMSAS_MailTempRem.equals("true")){
								flag = true;
							}
						}
						if(MsgP_SecTypeId.equals("18")){//烟度
							detailsenor = "烟度报警，"+MapGetter.getString(map, "MsgP_SensorValue");
							if(MsgP_State.equals("1")&&SMSAS_MailSmoke.equals("true")){
								flag = true;
							}
							if(MsgP_State.equals("3")&&SMSAS_MailSmokeRem.equals("true")){
								flag = true;
							}
						}
						
						if(flag){
							if(!SensorVcl.contains(VehicleNumber+",")){
								SensorVcl += VehicleNumber+",";
							}
							countSensor++;
							if(MsgP_State.equals("1")){
								detailsenor ="报警中，"+detailsenor; 
							}else if (MsgP_State.equals("3")){
								detailsenor ="已解除，"+detailsenor;
							}
							detail_sensor.append("<tr>");
							detail_sensor.append("<td>"+MapGetter.getString(map, "MsgP_ID")+"</td>");//ID
							detail_sensor.append("<td>"+VehicleNumber+"</td>");
							detail_sensor.append("<td>"+MsgP_MsgTime+"</td>");
							detail_sensor.append("<td> 传感器报警 </td>");
							detail_sensor.append("<td>"+detailsenor+"</td>");//详细信息
							detail_sensor.append("<td>"+MapGetter.getString(map, "MsgP_ReleaseTime")+"</td>");//解除时间
							detail_sensor.append("<td>"+release+"</td>");//解除方式
							detail_sensor.append("</tr>");
						}
					}
				}else if(MsgP_Type.equals("15") && SMSAS_MailFenTip.equals("true")){//进出围栏提示 3.6.6新增wangkai-20141020
					//判断进出围栏提示开关是否为关，如果为关则不处理
					if(!promptSwitch.get("7").contains(MsgP_VclID)){
						if(!FenTipVcl.contains(VehicleNumber+",")){
								FenTipVcl += VehicleNumber+",";
						}
						FenTipItem++;
						//获取设备位置
						String position = "暂无位置";
						/*通过经纬度获取位置信息并将位置更新到提示表Msg_Prompt*/
						String MsgP_Lo=MapGetter.getString(map, "MsgP_Lo");
						String MsgP_LA=MapGetter.getString(map, "MsgP_LA");
						try {
							position = MapFunction.getPositionReviseNoReplaceDescription(MsgP_Lo,MsgP_LA);
						} catch (Exception e) {
							//调用出错
							System.out.print("调用地图服务获得位置出错，" +
									"代码位置：DailyMaillService.getOtherPrompt(String dbCode,String collect,StringBuffer detail,Map<String,Object> user)");
						}
						position = position.replaceAll("省", "").replaceAll("市", "").replaceAll("附近","").replaceAll(" ","");
						String FenceName=MapGetter.getString(map, "FenceName");
						String Msgp_SecTypeID = MapGetter.getString(map, "Msgp_SecTypeID");
						
						detail_FenTip.append("<tr>");
						detail_FenTip.append("<td>"+VehicleNumber+"</td>");
						detail_FenTip.append("<td>"+MapGetter.getString(map, "ESNNumber")+"</td>");
						detail_FenTip.append("<td>"+MsgP_MsgTime+"</td>");
						detail_FenTip.append("<td>"+(Msgp_SecTypeID.equals("0")?"进围栏提示":Msgp_SecTypeID.equals("1")?"出围栏提示":"")+"</td>");
						detail_FenTip.append("<td>"+FenceName+"</td>");
						detail_FenTip.append("<td>"+position+"</td>");
						detail_FenTip.append("</tr>");
					}
				}else if(MsgP_Type.equals("16") && SMSAS_MailTankLevel.equals("true")){
					if(!TankVcl.contains(VehicleNumber+",")){
						TankVcl += VehicleNumber+",";
					}
					TankItem++;
					//获取设备位置
					String position = "暂无位置";
					/*通过经纬度获取位置信息并将位置更新到提示表Msg_Prompt*/
					String MsgP_Lo=MapGetter.getString(map, "MsgP_Lo");
					String MsgP_LA=MapGetter.getString(map, "MsgP_LA");
					try {
						position = MapFunction.getPositionReviseNoReplaceDescription(MsgP_Lo,MsgP_LA);
					} catch (Exception e) {
						//调用出错
						System.out.print("调用地图服务获得位置出错，" +
								"代码位置：DailyMaillService.getOtherPrompt(String dbCode,String collect,StringBuffer detail,Map<String,Object> user)");
					}
					position = position.replaceAll("省", "").replaceAll("市", "").replaceAll("附近","").replaceAll(" ","");
					detail_Tank.append("<tr>");
					detail_Tank.append("<td>"+VehicleNumber+"</td>");
					detail_Tank.append("<td>"+MsgP_MsgTime+"</td>");
					detail_Tank.append("<td>燃油异常减少</td>");
					detail_Tank.append("<td>"+position+"</td>");
					detail_Tank.append("<td>检测到油量异常减少，请关注。</td>");
					detail_Tank.append("</tr>");
				}
			}
			detail_Mtn.append("</table><br />");
			detail_FltRels.append("</table><br />");
			detail_Alm.append("</table><br />");
			detail_Mkbj.append("</table><br />");
			detail_sensor.append("</table><br />");
			detail_FenTip.append("</table><br />");
			detail_Tank.append("</table><br />");
			
			boolean isMX = false;
			if(SMSAS_MailTankLevel.equals("true") && TankItem>0){
				detail.append(detail_Tank);
				
				numList.put("TankVcl", TankVcl);
				numList.put("TankItem", TankItem);
				isMX = true;
			}
			if(SMSAS_MailMtn.equals("true")&& StringUtil.isValid(Mtn) && Mtn.split(",").length>0){
				detail.append(detail_Mtn);
				numList.put("Mtn", Mtn);
				
				isMX = true;
			}
			if((isFlt && FltItem>0) || (isRels && RelsItem>0)){
				detail.append(detail_FltRels);
				
				numList.put("FltItem", FltItem);
				numList.put("FltVcl", FltVcl);
				
				numList.put("RelsItem", RelsItem);
				numList.put("RelsVcl", RelsVcl);
				
				isMX = true;
			}
			if(SMSAS_MailAlm.equals("true") && AlmItem>0){
				detail.append(detail_Alm);
				
				numList.put("AlmVcl", AlmVcl);
				numList.put("AlmItem", AlmItem);
				
				isMX = true;
			}			
			if(SMSAS_MailMkbj.equals("true") && MkbjItem>0){
				detail.append(detail_Mkbj);
				
				numList.put("MkbjVcl", MkbjVcl);
				numList.put("MkbjItem", MkbjItem);
				
				isMX = true;
			}
			
			if((SMSAS_MailSmoke.equals("true")||SMSAS_MailSmokeRem.equals("true")
					||SMSAS_MailTemp.equals("true")||SMSAS_MailTempRem.equals("true")) && countSensor>0){
				detail.append(detail_sensor);
				
				numList.put("SensorVcl", SensorVcl);
				numList.put("SensorItem", countSensor);
				
				isMX = true;
			}
			
			if(SMSAS_MailFenTip.equals("true")&& FenTipItem>0){
					detail.append(detail_FenTip);
					numList.put("FenTipVcl", FenTipVcl);
					numList.put("FenTipItem", FenTipItem);
					isMX = true;
			}
			if(!isMX){
				detail.append("无<br /></br />");
			}
		}
		return getCollect(collect,user,numList);
	}
	
	/**
	 * 邮件汇总处内容
	 * @param collect
	 * @param user
	 * @param num
	 * @return
	 */
	public String getCollect(String collect,Map<String,Object> user,Map<String,Object> num){
		String SMSAS_MailMtn = MapGetter.getString(user, "SMSAS_MailMtn");	//保养提示
		String SMSAS_MailFlt = MapGetter.getString(user, "SMSAS_MailFlt");	//故障提示
		String SMSAS_MailRels = MapGetter.getString(user, "SMSAS_MailRels");	//故障解除提示
		String SMSAS_MailAlm = MapGetter.getString(user, "SMSAS_MailAlm");	//报警提示
		String SMSAS_MailBehavior = MapGetter.getString(user, "SMSAS_MailBehavior");	//驾驶行为提示
		String SMSAS_MailLoad = MapGetter.getString(user, "SMSAS_MailLoad");	//载重监控
		String SMSAS_MailMkbj = MapGetter.getString(user, "SMSAS_MailMkbj");	//门控报警
		String SMSAS_MailSmoke = MapGetter.getString(user, "SMSAS_MailSmokeAlarm");//烟度报警
		String SMSAS_MailSmokeRem = MapGetter.getString(user, "SMSAS_MailSmokeRels");//烟度报警解除
		String SMSAS_MailTemp = MapGetter.getString(user, "SMSAS_MailTempAlarm");//温度报警
		String SMSAS_MailTempRem = MapGetter.getString(user, "SMSAS_MailTempRels");//温度报警解除
		/*3.6.6新增进出围栏提示和时效预警 20141018-wangkai*/
		String SMSAS_MailFenTip = MapGetter.getString(user, "SMSAS_MailFenTip");//进出围栏提示
		String SMSAS_MailEffAlarm = MapGetter.getString(user, "SMSAS_MailEffAlarm");//时效预警
		
		String SMSAS_MailTankLevel=MapGetter.getString(user, "SMSAS_MailTankLevel");//液位传感器报警
		if(SMSAS_MailMtn.equals("true")){
			collect += "保养提示："+getString(num, "Mtn")+"台<br />";
		}
		if(SMSAS_MailFlt.equals("true")){
			collect += "故障提示："+getString(num, "FltVcl")+"台，"+getString(num, "FltItem")+"条<br />";
		}
		if(SMSAS_MailRels.equals("true")){
			collect += "解除提示："+getString(num, "RelsVcl")+"台，"+getString(num, "RelsItem")+"条<br />";
		}
		if(SMSAS_MailAlm.equals("true")){
			collect += "报警提示："+getString(num, "AlmVcl")+"台，"+getString(num, "AlmItem")+"条<br />";
		}
		if(SMSAS_MailMkbj.equals("true")){
			collect += "门控报警："+getString(num, "MkbjVcl")+"台，"+getString(num, "MkbjItem")+"条<br />";
		}
		if((SMSAS_MailSmoke.equals("true")||SMSAS_MailSmokeRem.equals("true")
				||SMSAS_MailTemp.equals("true")||SMSAS_MailTempRem.equals("true"))){
			collect += "传感器报警："+getString(num, "SensorVcl")+"台，"+getString(num, "SensorItem")+"条<br />";
		}
		//3.6.6新增进出围栏提示和时效预警20141020-wangkai
		if(SMSAS_MailFenTip.equals("true")){
			collect += "进出围栏提示："+getString(num, "FenTipItem")+"条<br />";
		}
		//4.19新增液位传感器报警 lvyingzhu
		if(SMSAS_MailTankLevel.equals("true")){
			collect +="燃油异常减少："+getString(num,"TankVcl")+"台，"+getString(num,"TankItem")+"条<br />";
		}
		return collect;
	}
	
	/**
	 * 分解详细信息
	 * @param type				提示类型:1-故障,2-报警,3-保养,4-司机行为,5-载重监控
	 * @param detailMsg
	 * @return
	 */
	public static Map<String,Object> getDetailMsg(String type,String detailMsg){
		Map<String ,Object> detailMsgs = new HashMap<String, Object>();
		String[] msgs = null; 
		if("1".equals(type)){//故障信息
			//黄色,135,04,xxxx,0
			msgs = (detailMsg+"<br/>0").split("<br/>");
			detailMsgs.put("CodeColor",msgs[0] );//代码颜色,该取值改变，不建议使用
			detailMsgs.put("FltCode",msgs[1] );//故障代码
			detailMsgs.put("LampStatus",msgs[2] );//指示灯
			detailMsgs.put("FltContent",msgs[3] );//故障信息
			detailMsgs.put("FltTimes",msgs[4] );//报警次数 2013.05.27 haoyang add
		}else if("2".equals(type)){//报警信息
			detailMsgs.put("AlarmType",detailMsg );//详细信息
		}else if("3".equals(type)){//保养信息
			//A级,10,10,20,20,123,0
			//jiyongtian 20140628 IEMS3.6 新保养逻辑调整
			if(detailMsg.indexOf(",")>-1){
				msgs = detailMsg.split(",");
			}else{
				msgs = detailMsg.split("，");
			}
//			msgs = (detailMsg+"，0").split("，");
			detailMsgs.put("MtnType",msgs[0] );//保养类型
			detailMsgs.put("WorkHour",msgs[1] );//提醒时 工作小时数
			detailMsgs.put("RunKm",msgs[2] );//提醒时 行驶公里数
			/*detailMsgs.put("AlarmHour",msgs[3] );//距保 工作小时数
			detailMsgs.put("AlarmKm",msgs[4] );//距保 行驶公里数
			detailMsgs.put("MtnFSID",msgs[5] );//保养标准ID
			if(msgs.length>7){
				detailMsgs.put("MtnGapDays",msgs[6] );//提示时间隔天数
				detailMsgs.put("AlarmDays",msgs[7] );//距保养天数	
			}else{
				detailMsgs.put("MtnGapDays","" );//提示时间隔天数
				detailMsgs.put("AlarmDays","" );//距保养天数
			}
			*/
		}else if("4".equals(type)){//司机行为
			msgs = detailMsg.split("，");
			// 详细信息
			String DtrTipContent = "";
			for (int di = 1; di < msgs.length; di++) {
				DtrTipContent += msgs[di];
				if (di != msgs.length - 1) {
					DtrTipContent += "<br/>";
				}
			}
			detailMsgs.put("DtrType",msgs[0] );//行为提示类型
			detailMsgs.put("DtrTipContent",DtrTipContent );//详细信息
		}else if("5".equals(type)){//载重监控
			msgs = detailMsg.split("→");
			if(msgs.length==2){
				detailMsgs.put("BfrStatus",msgs[0] );//行为提示类型
				detailMsgs.put("AftStatus",msgs[1] );//详细信息
			}
		}
		return detailMsgs;
	}
	
	public static String getString(Map map, String key){
		if(map==null||map.size()<=0){
			return "0";
		}
		try{
			String reValue = "0";
			Object objValue=map.get(key);
			if(null == objValue){
				return reValue;
			}
			
			if(objValue instanceof String){
				reValue = (String) objValue;
				if(reValue.contains(",")){
					reValue = objValue.toString().split(",").length+"";
				}else if(!StringUtil.isValid(reValue)){
					return "0";
				}
			}else
			if(objValue instanceof Integer){
				reValue = String.valueOf(((Integer) objValue).intValue());
			}else{
				reValue = objValue.toString();
			}
				
			return reValue;
		}catch(Exception e){
			 e.printStackTrace();
		}
		
		return "";
	}
	/**
	 * 获取指示灯状态
	 * @param fault
	 * @return
	 */
	public String getLampStatus(Map<String, Object> fault) {
		// 指示灯状态 xxxx
		//第1位 		故障指示灯
		//第2位			红色停止灯
		//第3位    		琥珀色警告灯
		//第4位			保护灯
		String LampStatus = "";
		String lamp = MapGetter.getString(fault,
				"LampStatus");
		if(StringUtil.isNullOrEmpty(lamp)&&lamp.length()==4){
		if('1'==lamp.charAt(0)){
			LampStatus += "，故障指示灯ON";
		}
		if('1'==lamp.charAt(1)){
			LampStatus += "，红色停止灯ON";
		}
		if('1'==lamp.charAt(2)){
			LampStatus += "，琥珀色警告灯ON";
		}
		if('1'==lamp.charAt(3)){
			LampStatus += "，保护灯ON";
		}
		}
		if(LampStatus.length()>1)
			return LampStatus.substring(1,LampStatus.length());
		else
			return "无";
	}
	/**
	 * 获取故障颜色
	 * @param fault
	 * @return
	 */
	public String getFltColor(Map<String, Object> fault) {
		String FltColor = "";
		String color = MapGetter.getString(fault,
				"CodeColor");
		if(StringUtil.isNullOrEmpty(color)){
			if("红色".equals(color)){
				FltColor = "红色报警";
			}else if("黄色".equals(color)){
				FltColor = "黄色报警";
			}else if("蓝色".equals(color)){
				FltColor = "蓝色报警";
			}else{
				FltColor = "其他";
			}
		}else{
			FltColor += "，其他";
		}
		return FltColor;
	}
}

